package com.pzhu.record.controller;

import com.pzhu.record.entity.Basicinformation;
import com.pzhu.record.entity.Punish;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author Zhou YuSen
 * @date 2020/4/13 9:22
 */
@Controller
@RequestMapping("/punish")
public class PunishController extends BaseAction {

    @RequestMapping("/insert")
    public String insert(Punish punish, Model model, MultipartFile pic, HttpServletRequest request, HttpSession session) throws IOException {
        String filename = pic.getOriginalFilename();
        if (filename != null && filename.length() > 0) {
            String path = request.getServletContext().getRealPath("/upload");
            String filePunishName = path + "\\" + filename;
            File file = new File(filePunishName);
            pic.transferTo(file);
            punish.setPunishPaper(filePunishName);
        }
        String basicinformationId = punish.getBasicinformationId();
        Basicinformation basicinformation = basicinformationService.selectById(basicinformationId);
        if (basicinformation == null) {
            model.addAttribute("messagePunish", "学号错误，无此人");
            return "nav";
        }

        int insert = punishService.insertSelective(punish);
        if (insert != 1) {
            model.addAttribute("messagePunish", "添加失败");
        } else {
            model.addAttribute("messagePunish", "添加成功");
        }
        return "nav";
    }

}
